<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/4
 * Time: 17:20
 */

namespace Home\Controller;
use DH16\Libs\Controller;
use DH16\Tools\Model;

/**
 * Class StudentController
 * @package Home\Controller
 * 学生管理控制器
 */
class StudentController extends Controller
{
    private $model;  //数据库对象

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new Model();
    }

    /**
     * 首页
     */
    public function index(){
        //2.接收查看班级的id号
        $cid = $_GET['cid'];
        //3.获取所有数据
        $allStudent = $this->model->query("select * from stu where cid={$cid}");
        //4.分配变量
        $this->assign('allStudent',$allStudent);
        //1.载入模板
        $this->display();
    }

    /**
     * 添加学生
     */
    public function addStudent(){
        //2.获得班级id
        $cid = $_GET['cid'];
        //3.获得班级名称
        $cname = $this->model->query("select cname from class where cid={$cid}");
        $cname =$cname[0]['cname'];
        //4.分配变量
        $this->assign('cname',$cname);
        //5.判断
        if(IS_POST){
            //6.获得所有数据
            $sname = $_POST['sname'];
            $age =$_POST['age'];
            $sex = $_POST['sex'];
            //7.添加数据
            $rows = $this->model->exec("insert into stu set sname='{$sname}',sex='{$sex}',age={$age},cid={$cid}");
            //8.提示信息
            if($rows){
                $this->success('添加成功',"index.php?c=student&a=index&cid={$cid}");
            }else{
                $this->error('添加失败');
            }
        }
        //1.载入模板
        $this->display();
    }

    /**
     * 编辑
     */
    public function editStudent(){
        //2.获得班级id和学生id
        $cid = $_GET['cid'];
        $sid = $_GET['sid'];
        //3.获得旧数据
        $oldStudent = $this->model->query("select * from stu where sid={$sid}");
        $oldStudent = $oldStudent[0];
        //4.分配变量
        $this->assign('oldStudent',$oldStudent);
        //5.获得所有班级名称
        $allClass = $this->model->query("select * from class");
        //6.分配变量
        $this->assign("allClass",$allClass);

        //7.判断
        if(IS_POST){
            //8.获得所有数据
            $sname = $_POST['sname'];
            $age = $_POST['age'];
            $sex = $_POST['sex'];
            $cid = $_POST['cid'];

            //9.修改数据
            $rows = $this->model->exec("update stu set sname='{$sname}',sex='{$sex}',age={$age},cid={$cid} where sid={$sid}");
            //10.提示信息
            if($rows){
                $this->success("编辑成功","index.php?c=student&a=index&cid={$cid}");
            }else{
                $this->error("编辑失败");
            }
        }

        //1.载入模板
        $this->display();
    }

    /**
     * 删除学生
     */
    public function deleteStudent(){
        //1.获得班级id和学生id
        $sid = $_GET['sid'];
        $cid = $_GET['cid'];
        //2.删除数据
        $rows = $this->model->exec("delete from stu where sid={$sid}");
        //3.提示信息
        if($rows){
            $this->success("删除成功","index.php?c=student&a=index&cid={$cid}");
        }else{
            $this->error('删除失败');
        }
    }

}